<template>
  <a-layout style="height: 100%">
    <a-layout-content>
      <VxeBasicTable ref="TableRef" v-bind="options">
        <template #toolbar_buttons>
          <ToolBtn
            :filter-call="() => {}"
            :create-call="handleEdit"
            :delete-call="() => {}"
            :show-export="false"
            :show-import="false"
            :show-delete="false"
            :show-filter="false"
            @start-all-job="handleStartAllJob"
            @pause-all-job="handlePauseAllJob"
            @cancel-sleep="handleCancelSleep"
          />
        </template>
        <template #action="{ row }">
          <Action
            :record="row"
            @edit="handleEdit"
            :delete-call="handleDelete"
          />
        </template>
      </VxeBasicTable>
      <EditJobDetailModal
        ref="EditJobDetailModalRef"
        v-model:visible="showJobDetailEdit"
        :data="current"
        @done="reload"
      />
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { storeToRefs } from 'pinia';
  import { useThemeStore } from '@/store/modules/theme';
  import {
    getJobPageList,
    startAllJob,
    pauseAllJob,
    cancelSleep,
    persistAll,
    startJob,
    pauseJob,
    // startTrigger,
    // pauseTrigger,
    deleteJob
  } from '@/api/system/job';
  import { VxeBasicTable } from '@/components/VxeTable';
  import EditJobDetailModal from './components/EditJobDetailModal.vue';
  import { hasPermission } from '@/utils/permission';
  import { message } from 'ant-design-vue';
  import JobTriggerData from './components/JobTriggerData';
  import type { SysJobDetail } from '@/api/system/job/model';
  import { useConfig } from './config';

  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);
  const { TableRef, ToolBtn, Action, options } = useConfig(getJobPageList);

  const EditJobDetailModalRef = ref();
  const showJobTriggerEdit = ref(false);
  const showJobDetailEdit = ref(false);
  // 当前选中数据
  const current = ref<SysJobDetail>();

  const reload = () => {
    TableRef.value?.commitProxy('query');
  };

  function handleEdit(_record) {
    current.value = _record?.jobDetail;
    showJobDetailEdit.value = true;
    //const { jobDetail } = record;
  }
  function handleCreateTrigger() {
    showJobTriggerEdit.value = true;
  }
  // function handleEditTrigger(record: Recordable) {}

  async function handleDelete(record) {
    deleteJob(record.id)
      .then((res) => {
        message.success(res);
        reload();
      })
      .catch((error) => {
        message.error(error);
      });
  }
  function handleStartAllJob() {
    startAllJob()
      .then((res) => {
        message.success(res);
      })
      .catch((error) => {
        message.error(error);
      });
  }
  function handlePauseAllJob() {
    pauseAllJob()
      .then((res) => {
        message.success(res);
      })
      .catch((error) => {
        message.error(error);
      });
  }
  function handleCancelSleep() {
    cancelSleep()
      .then((res) => {
        message.success(res);
      })
      .catch((error) => {
        message.error(error);
      });
  }
  function handlePersistAll() {
    persistAll()
      .then((res) => {
        message.success(res);
      })
      .catch((error) => {
        message.error(error);
      });
  }
  function handleStartJob(record: { jobDetail: { jobId: any } }) {
    startJob({ jobId: record.jobDetail?.jobId })
      .then((res) => {
        message.success(res);
      })
      .catch((error) => {
        message.error(error);
      });
  }
  // function handleStartTrigger(record: { jobId: any; triggerId: any }) {
  //   startTrigger({ jobId: record.jobId, triggerId: record.triggerId })
  //     .then((res) => {
  //       message.success(res);
  //     })
  //     .catch((error) => {
  //       message.error(error);
  //     });
  // }
  function handlePauseJob(record: { jobDetail: { jobId: any } }) {
    pauseJob({ jobId: record.jobDetail?.jobId })
      .then((res) => {
        message.success(res);
      })
      .catch((error) => {
        message.error(error);
      });
  }
  // function handlePauseTrigger(record: { jobId: any; triggerId: any }) {
  //   pauseTrigger({ jobId: record.jobId, triggerId: record.triggerId })
  //     .then((res) => {
  //       message.success(res);
  //     })
  //     .catch((error) => {
  //       message.error(error);
  //     });
  // }
</script>

<script lang="ts">
  export default {
    name: 'SysTimer'
  };
</script>
